home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 10 / des.zip / DES.C next >
Text File  |  1989-08-27  |  12KB  |  402 lines

  1. /*
  2.  *    des.c
  3.  *
  4.  *    Software Data Encryption Standard Implementation
  5.  *
  6.  *    Version 2.4, February 25, 1985
  7.  *
  8.  *    Permission is hereby given for non-commercial use.
  9.  *
  10.  *    Copyright (c) 1977, 1984, 1985 By
  11.  *    Jim Gillogly, Lauren Weinstein, and Richard Outerbridge
  12.  */
  13.  
  14. #define    DE    1
  15. typedef    char    BYTE;    /* BYTE = (vax) ? int : char    */
  16.  
  17. void        desinit(), p48init(), permute();
  18. static char    s12[4096], s34[4096], s56[4096], s78[4096];
  19. static long    p48a[256][2], p48b[256][2], p48c[256][2], p48d[256][2];
  20. static long    kn[32];
  21.  
  22. static int pmv[8] = {
  23.     64, 16,  4,  1, 128, 32,  8,  2
  24. };
  25.  
  26. static int pvs[8] = {
  27.      1,  2,  4,  8,  16, 32, 64, 128
  28. };
  29.  
  30.  
  31. void des(inblock, outblock)
  32. BYTE    *inblock, *outblock;
  33. {
  34.     register long    *fmp, suba, subb, val, *h0L, *h1R, *keys;
  35.         BYTE        scratch[8], *small;
  36.         long        swap[4];
  37.         int        i;
  38.  
  39.         permute(inblock, pmv, pvs, scratch);
  40.  
  41.         h0L = swap;
  42.     h1R = &swap[4];
  43.     small = scratch;
  44.  
  45.         while (h0L < h1R)  {
  46.                 val  = ((*small++) & 0377L) << 24;
  47.         val |= ((*small++) & 0377L) << 16;
  48.                 val |= ((*small++) & 0377L) << 8;
  49.         val |= (*small++)  & 0377L;
  50.  
  51.                 *h0L++ = ((val & 0x1L) << 23) | ((val >> 9) & 0x7C0000L) |
  52.                          ((val & 0x1F800000L) >> 11) | 
  53.                          ((val & 0x1F80000L)  >> 13) |
  54.                          ((val & 0x1F8000L)   >> 15);
  55.                 *h0L++ = ((val & 0x1F800L)    <<  7) |
  56.                          ((val & 0x1F80L)     <<  5) |
  57.                          ((val & 0x1F8L)      <<  3) |
  58.                          ((val & 0x1FL)       <<  1) |
  59.              ((val >> 31) & 0x1L);
  60.     }
  61.  
  62.         keys = kn;
  63.     h0L = swap;
  64.     h1R = &swap[2];
  65.  
  66.         for (i = 0; i < 16; i++)  {
  67.                 val = *keys++ ^ *h1R++;
  68.                 fmp = &p48a[ s12[(val >> 12)] & 0377 ][0];
  69.                 suba = *fmp++;
  70.         subb = *fmp;
  71.                 fmp = &p48b[ s34[(val & 07777L)] & 0377 ][0];
  72.                 suba |= *fmp++;
  73.         subb |= *fmp;
  74.                 val = *keys++ ^ *h1R++;
  75.                 fmp = &p48c[ s56[(val >> 12)] & 0377 ][0];
  76.                 suba |= *fmp++;
  77.         subb |= *fmp;
  78.                 fmp = &p48d[ s78[(val & 07777L)] & 0377 ][0];
  79.                 suba |= *fmp++;
  80.         subb |= *fmp;
  81.                 *h0L++ ^= suba;
  82.         *h0L++ ^= subb;
  83.  
  84.                 if (i & 1)
  85.             h0L = swap;
  86.                 else
  87.             h1R = swap;
  88.     }
  89.  
  90.         val = *h0L;
  91.     *h0L++ = *h1R;
  92.     *h1R++ = val;
  93.         val = *h0L;
  94.     *h0L = *h1R;
  95.     *h1R = val;
  96.         h0L = swap;
  97.     h1R = &swap[4];
  98.     small = scratch;
  99.  
  100.         while (h0L < h1R)  {
  101.                 *small++ = ((*h0L & 036000000L) >> 15) |
  102.                ((*h0L & 0360000L) >> 13);
  103.                 *small++ = ((*h0L & 03600L) >> 3) |
  104.                ((*h0L & 036L) >> 1);
  105.                 h0L++;
  106.         }
  107.  
  108.         permute(scratch, pvs, pmv, outblock);
  109. }
  110.  
  111. static char s1[64] = {                  /* S[1]<<4              */
  112.        224, 64,208, 16, 32,240,176,128, 48,160, 96,192, 80,144,  0,112,
  113.          0,240,112, 64,224, 32,208, 16,160, 96,192,176,144, 80, 48,128,
  114.         64, 16,224,128,208, 96, 32,176,240,192,144,112, 48,160, 80,  0,
  115.        240,192,128, 32, 64,144, 16,112, 80,176, 48,224,160,  0, 96,208
  116. };
  117.  
  118. static char s3[64] = {                  /* S[3]<<4              */
  119.        160,  0,144,224, 96, 48,240, 80, 16,208,192,112,176, 64, 32,128,
  120.        208,112,  0,144, 48, 64, 96,160, 32,128, 80,224,192,176,240, 16,
  121.        208, 96, 64,144,128,240, 48,  0,176, 16, 32,192, 80,160,224,112,
  122.         16,160,208,  0, 96,144,128,112, 64,240,224, 48,176, 80, 32,192
  123. };
  124.  
  125. static char s5[64] = {                  /* S[5]<<4              */
  126.         32,192, 64, 16,112,160,176, 96,128, 80, 48,240,208,  0,224,144,
  127.        224,176, 32,192, 64,112,208, 16, 80,  0,240,160, 48,144,128, 96,
  128.         64, 32, 16,176,160,208,112,128,240,144,192, 80, 96, 48,  0,224,
  129.        176,128,192,112, 16,224, 32,208, 96,240,  0,144,160, 64, 80, 48
  130. };
  131.  
  132. static char s7[64] = {                  /* S[7]<<4              */
  133.         64,176, 32,224,240,  0,128,208, 48,192,144,112, 80,160, 96, 16,
  134.        208,  0,176,112, 64,144, 16,160,224, 48, 80,192, 32,240,128, 96,
  135.         16, 64,176,208,192, 48,112,224,160,240, 96,128,  0, 80,144, 32,
  136.         96,176,208,128, 16, 64,160,112,144, 80,  0,240,224, 32, 48,192
  137. };
  138.  
  139. static char s2[64] = {                  /* S[2]                 */
  140.         15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  141.          3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  142.          0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  143.         13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9
  144. };
  145.  
  146. static char s4[64] = {                  /* S[4]                 */
  147.          7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  148.         13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  149.         10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  150.          3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14
  151. };
  152.  
  153. static char s6[64] = {                  /* S[6]                 */
  154.         12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  155.         10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  156.          9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  157.          4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13
  158. };
  159.  
  160. static char s8[64] = {                  /* S[8]                 */
  161.         13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  162.          1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  163.          7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  164.          2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  165. };
  166.  
  167. static char p48i[] = {
  168.         24, 15,  6, 19, 20, 28, 20, 28, 11, 27, 16,  0,
  169.         16,  0, 14, 22, 25,  4, 25,  4, 17, 30,  9,  1,
  170.          9,  1,  7, 23, 13, 31, 13, 31, 26,  2,  8, 18,
  171.          8, 18, 12, 29,  5, 21,  5, 21, 10,  3, 24, 15
  172. };
  173.  
  174. static int bytebit[] = {        /* bit 0 is left-most in byte   */
  175.     0200,0100,040,020,010,04,02,01
  176. };
  177.  
  178. static long bigbyte[] = {
  179.         0x800000L, 0x400000L, 0x200000L, 0x100000L,
  180.         0x80000L,  0x40000L,  0x20000L,  0x10000L,
  181.         0x8000L,   0x4000L,   0x2000L,   0x1000L,
  182.         0x800L,    0x400L,    0x200L,    0x100L,
  183.         0x80L,     0x40L,     0x20L,     0x10L,
  184.         0x8L,      0x4L,      0x2L,      0x1L
  185. };
  186.  
  187. static int Ignited = 0;
  188.  
  189. void desinit()
  190. {
  191.     register int    j, left, right;
  192.  
  193.         if (Ignited)
  194.         return;
  195.  
  196.         for (j = 0; j < 4096; j++)  {
  197.                 left  = ((j >> 6) & 040) | ((j >> 2) & 020) | ((j >> 7) & 017);
  198.                 right = (j & 040) | ((j << 4) & 020) | ((j >> 1) & 017);
  199.                 s12[j] = s1[left] | s2[right];
  200.                 s34[j] = s3[left] | s4[right];
  201.                 s56[j] = s5[left] | s6[right];
  202.                 s78[j] = s7[left] | s8[right];
  203.     }
  204.  
  205.         p48init();
  206.         Ignited = 1;
  207. }
  208.  
  209. #ifdef KSX64
  210.  
  211. /* Implement an experimental, non-standard, simple-minded 64-bit key.   */
  212.  
  213. void kinit(key, edf)
  214. BYTE    *key;
  215. int    edf;
  216. {
  217.         register int    i;
  218.         register long    *tofill;
  219.         long        master[16];
  220.  
  221.         if (!Ignited)
  222.         desinit(); 
  223.  
  224.         for (i = 0; i < 16; i++)  {
  225.                 master[i++] = (*key >> 4) & 017L;
  226.                 master[i] = (*key++) & 017L;
  227.         }
  228.  
  229.         for (i = 0; i < 16; i++)  {
  230.                 if (edf == DE)
  231.             tofill = &kn[(15 - i) << 1];
  232.                 else
  233.             tofill = &kn[i << 1];
  234.  
  235.                 *tofill  = (master[(i +  1) & 017] << 20);
  236.                 *tofill |= (master[(i +  8) & 017] << 16);
  237.                 *tofill |= (master[(i +  5) & 017] << 12);
  238.                 *tofill |= (master[(i + 10) & 017] <<  8);
  239.                 *tofill |= (master[(i +  3) & 017] <<  4);
  240.                 *tofill++ |= master[(i + 14) & 017];
  241.                 *tofill  = (master[(i +  6) & 017] << 20);
  242.                 *tofill |= (master[(i + 11) & 017] << 16);
  243.                 *tofill |= (master[(i +  2) & 017] << 12);
  244.                 *tofill |= (master[(i + 13) & 017] <<  8);
  245.                 *tofill |= (master[i] << 4);
  246.                 *tofill |=  master[(i + 9) & 017];
  247.     }
  248. }
  249.  
  250. #else
  251.  
  252. /* Use the key schedule specified in the Standard (ANSI X3.92-1981).    */
  253.  
  254. static char pc1[] = {                   /* permuted choice table (key)  */
  255.         56, 48, 40, 32, 24, 16,  8,
  256.          0, 57, 49, 41, 33, 25, 17,
  257.          9,  1, 58, 50, 42, 34, 26,
  258.         18, 10,  2, 59, 51, 43, 35,
  259.         62, 54, 46, 38, 30, 22, 14,
  260.          6, 61, 53, 45, 37, 29, 21,
  261.         13,  5, 60, 52, 44, 36, 28,
  262.         20, 12,  4, 27, 19, 11,  3
  263. };
  264.  
  265. static char totrot[] = {                /* number left rotations of pc1 */
  266.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  267. };
  268.  
  269. static char pc2[] = {                   /* permuted choice key (table)  */
  270.         13, 16, 10, 23,  0,  4,
  271.          2, 27, 14,  5, 20,  9,
  272.         22, 18, 11,  3, 25,  7,
  273.         15,  6, 26, 19, 12,  1,
  274.         40, 51, 30, 36, 46, 54,
  275.         29, 39, 50, 44, 32, 47,
  276.         43, 48, 38, 55, 33, 52,
  277.         45, 41, 49, 35, 28, 31
  278. };
  279.  
  280. void kinit(key, edf)                    /* initialize key schedule array*/
  281. BYTE    *key;                /* 64 bits; we'll only use 56   */
  282. int    edf;
  283. {
  284.         register int    i, j, l, m, n;
  285.         char        pc1m[56], pcr[56];
  286.  
  287.         if (!Ignited)
  288.         desinit();
  289.  
  290.         for (j = 0; j < 56; j++)  {    /* convert pc1 to bits of key   */
  291.                 l = pc1[j];             /* integer bit location         */
  292.                 m = l & 07;             /* find bit                     */
  293.                 pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
  294.         }
  295.  
  296.         for (i = 0; i < 16; i++)  {    /* key chunk for each iteration */
  297.                 if (edf == DE)
  298.             m = (15 - i) << 1;
  299.                 else
  300.             m = i << 1;
  301.  
  302.                 n = m + 1;
  303.                 kn[m] = kn[n] = 0L;
  304.  
  305.                 for (j = 0; j < 56; j++)  /* rotate pc1 the right amount  */
  306.             pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l: l - 28];
  307.  
  308.                 /* rotate left and right halves independently   */
  309.  
  310.                 for (j = 0; j < 24; j++)  {
  311.                         if (pcr[pc2[j]])
  312.                 kn[m] |= bigbyte[j];
  313.                         if (pcr[pc2[j + 24]])
  314.                 kn[n] |= bigbyte[j];
  315.             }
  316.     }
  317. }
  318.  
  319. #endif
  320.  
  321. static void p48init()        /* initialize 32-bit permutation */
  322. {
  323.         register int    l, j, k, i;
  324.         register long    (*pfill)[2];
  325.  
  326.         for (i = 0; i < 4; i++)  {    /* each input byte position     */
  327.                 switch (i)  {
  328.         default:
  329.             break;
  330.         case 0:
  331.             pfill = p48a;
  332.             break;
  333.         case 1:
  334.             pfill = p48b;
  335.                         break;
  336.                 case 2:
  337.             pfill = p48c;
  338.                         break;
  339.                 case 3:
  340.             pfill = p48d;
  341.                 }
  342.  
  343.                 for (j = 0; j < 256; j++)  {
  344.                         for (k = 0; k < 2; k++)
  345.                 pfill[j][k] = 0L;
  346.  
  347.                         for (k = 0; k < 24; k++)  {
  348.                                 l = p48i[k];
  349.                                 if ((l >> 3) != i)
  350.                     continue;
  351.  
  352.                                 if (!(j & bytebit[l & 07]))
  353.                     continue;
  354.  
  355.                                 pfill[j][0] |= bigbyte[k];
  356.                         }
  357.  
  358.                         for (k = 24; k < 48; k++)  {
  359.                                 l = p48i[k];
  360.                                 if ((l >> 3) != i)
  361.                     continue;
  362.  
  363.                                 if (!(j & bytebit[l & 07]))
  364.                     continue;
  365.  
  366.                                 pfill[j][1] |= bigbyte[k - 24];
  367.                         }
  368.                 }
  369.         }
  370. }
  371.  
  372. static void permute(inblock, test, vals, outblock)
  373. register BYTE    *inblock;
  374. int        *test;
  375. register int    *vals;
  376. BYTE        *outblock;
  377. {
  378.         register BYTE    *cp, *eop, *eip;
  379.         register int    *dp;
  380.  
  381.         eop = &outblock[8];
  382.     eip = &inblock[8];
  383.  
  384.         for (cp = outblock; cp < eop;)
  385.         *cp++ = 0;
  386.  
  387.         while (inblock < eip)  {
  388.                 cp = outblock;
  389.                 dp = test;
  390.  
  391.                 while (cp < eop)  {
  392.                         if (*inblock & *dp++)
  393.                 *cp |= *vals;
  394.  
  395.                         cp++;
  396.                 }
  397.  
  398.                 inblock++;
  399.                 vals++;
  400.         }
  401. }
  402.